﻿using FarmSmartSleep.Common.Domain.Sales_Purchase;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.Sales_Purchase.PersonalProcurement
{
    /// <summary>
    /// 个人采购
    /// </summary>
    public class PersonalProcurementCreateHandler:IRequestHandler<PersonalProcurementCreateCommon,Result>
    {
        private readonly IBaseRepository<Proc> procRep;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="procRep"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public PersonalProcurementCreateHandler(IBaseRepository<Proc> procRep)
        {
            this.procRep = procRep ?? throw new ArgumentNullException(nameof(procRep));
        }
        /// <summary>
        /// 添加的处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(PersonalProcurementCreateCommon request, CancellationToken cancellationToken)
        {
            var result = new Result { Code = (int)CodeEnum.add };
            try
            {
                // 尝试添加数据
                var n = await procRep.AddRange(request.proc); 
                if(n==0)
                {
                    result.Messege = "添加失败";
                    return result;
                }
                result.Code = (int)CodeEnum.success;
                result.Messege = "添加成功"; 
            }
            catch (Exception)
            {
                result.Messege = "添加";
                throw;
            }
            return result;
        }
    }
}
